<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="园区" prop="area">-->
<!--        <el-select v-model="queryParams.area" placeholder="请选择园区"  style="width: 240px" @change="handleQuery">-->
<!--          <el-option label="全部" value="" ></el-option>-->
<!--          <el-option v-for="item in dict.type.user_area" :label="item.label" :value="item.value" ></el-option>-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item label="核算单号" prop="businessNum">
        <el-input v-model="queryParams.businessNum" placeholder="请输入结算单号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="settlementList" @selection-change="handleSelectionChange">
      <el-table-column type="index" label="序号" width="55" align="center" />
      <el-table-column label="核算单号" align="center" prop="businessNum" min-width="150"/>
      <el-table-column label="园区" align="center" prop="area" min-width="100"/>
      <el-table-column label="核算日期" align="center" prop="settlementTime" min-width="120">
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.settlementTime, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
      </el-table-column>
<!--      <el-table-column label="核算总金额" align="center" prop="money" width="140" >-->
<!--        <template v-slot="{row}">-->
<!--          {{row.money | currency}}-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="本期总收入" align="center" prop="staffUseTotal" min-width="140" >
        <template v-slot="{row}">
          {{row.staffUseTotal | currency}}
        </template>
      </el-table-column>
      <el-table-column label="本期总支出" align="center" prop="householdUseTotal" min-width="140" >
        <template v-slot="{row}">
          {{row.householdUseTotal | currency}}
        </template>
      </el-table-column>
      <el-table-column label="本期营收余额" align="center" prop="staffCaseTotal" min-width="140" >
        <template v-slot="{row}">
          {{row.staffCaseTotal | currency}}
        </template>
      </el-table-column>
      <el-table-column label="员工账户余额总计" align="center" prop="householdCaseTotal" min-width="140" >
        <template v-slot="{row}">
          {{row.householdCaseTotal | currency}}
        </template>
      </el-table-column>
      <el-table-column label="住户账户余额总计" align="center" prop="staffCardTotal" min-width="140" >
        <template v-slot="{row}">
          {{row.staffCardTotal | currency}}
        </template>
      </el-table-column>
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120">-->
<!--        <template slot-scope="scope">-->
<!--&lt;!&ndash;          <el-button&ndash;&gt;-->
<!--&lt;!&ndash;            size="mini"&ndash;&gt;-->
<!--&lt;!&ndash;            type="text"&ndash;&gt;-->
<!--&lt;!&ndash;            icon="el-icon-edit"&ndash;&gt;-->
<!--&lt;!&ndash;            @click="handleUpdate(scope.row)"&ndash;&gt;-->
<!--&lt;!&ndash;            v-hasPermi="['system:settlement:edit']"&ndash;&gt;-->
<!--&lt;!&ndash;          >修改</el-button>&ndash;&gt;-->
<!--&lt;!&ndash;          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>&ndash;&gt;-->
<!--        </template>-->
<!--      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open" width="400px" append-to-body :close-on-click-modal="false" @close="cancel">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="期末核算月份:" prop="settlementTime">
          <el-date-picker  v-model="form.settlementTime" placeholder="请选择月份" type="month" align="right"
                           format="yyyy年MM月" value-format="yyyy-MM"  :clearable="false">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" >确 定</el-button>
        <el-button @click="cancelForm">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listSettlement,
  getSettlement,
  delSettlement,
  addSettlement,
  updateSettlement,
  createdSettlement
} from "@/api/settlement/daily";
import {createSettlement} from "@/api/settlement/supplier";
import {getStoreDetailList, saveNewStorePan} from "@/api/store/calculate/calculate";
import {createPeriodEnd, periodEndList} from "@/api/settlement/periodEnd";
import {changeRule} from "@/utils/formRules";

export default {
  name: "Settlement",
  dicts:['user_area'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      settlementList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        businessNum: null,
        // area: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        settlementTime:changeRule('请选择期末核算月份')
      }
    };
  },
  created() {
    // this.queryParams.area=this.$store.state.user.userInfo.area || ''
    this.getList();
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      periodEndList(this.queryParams).then(response => {
        this.settlementList = response.rows;
        this.total = response.total;
      }).finally(()=>{
        this.loading = false;

      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      // this.form = {
      //   id: null,
      //   businessNum: null,
      //   area: null,
      //   settlementTime: null,
      //   money: null,
      //   createBy: null,
      //   createTime: null,
      //   staffUseTotal: null,
      //   householdUseTotal: null,
      //   staffCaseTotal: null,
      //   householdCaseTotal: null,
      //   staffCardTotal: null,
      //   householdCardTotal: null,
      //   staffBuckleTotal: null,
      //   householdBuckleTotal: null
      // };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open=true
      this.title='新增期末核算'
    },
    cancelForm(){
      this.resetForm("form");
      this.open=false
    },
    submitForm(){
      this.$refs["form"].validate(valid => {
        if(valid){
          this.$confirm('请确认是否生成期末核算单').then(res=> {
            createPeriodEnd(this.form).then(res => {
              this.$message.success('新增成功')
              this.cancelForm()
              this.getList()
            })
          })
        }
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSettlement(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改【请填写功能名称】";
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('请确认是否删除该每日结算单').then(function() {
        return delSettlement(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/settlement/export', {
        ...this.queryParams
      }, `settlement_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
